package com.jiepos.mpos.inter.intopieces.service;

import java.util.List;

import org.apache.commons.httpclient.NameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.jiepos.mpos.biz.channel.service.InfChannelService;
import com.jiepos.mpos.biz.customer.service.AppCustomerInfoService;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.core.util.GlobalUtils;
import com.jiepos.mpos.core.util.HttpUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * @ClassName: MposIntoPieces
 * @Description: 钱包后台管理系统进件管理类
 * @author 孙永
 * @date 2018年1月25日 上午11:04:08
 */
@Component
public class IntoPieces {
	/** 修改结算卡接口类型：modifyAccount*/
	private static final String MODIFY_ACCOUNT_TYPE = "modifyAccount";
	/** 参数操作人的key：operater*/
	private static final String OPERATER = "operater";
	/** 参数渠道编号的key：channelNo*/
	private static final String CHANNEL_NO = "channelNo";
	/** 参数会员编号的key：customerNo*/
	private static final String CUSTOMER_NO = "customerNo";
	/** 参数进件类型的key：intoType*/
	private static final String INTO_TYPE = "intoType";
	
	private static Logger logger = LoggerFactory.getLogger(IntoPieces.class);
	
	@Autowired
	private InfChannelService infChannelService;
	@Autowired
	private AppCustomerInfoService customerInfoService;
	
	/**
	 * 根据会员标识上送会员新的结算信息
	 * @param customerUuid 会员标识
	 * @param operater 操作人
	 * @param channels 渠道，默认全部上送
	 */
	@Async
	public void modifyAccountByUuid(String customerUuid, String operater, String...channels){
		if(StringUtils.isNotBlank(customerUuid)){
			App_Custom_InfEntity customer = customerInfoService.getCustomByCustomUUID(customerUuid);
			if(customer != null && StringUtils.isNotBlank(customer.getCustom_no())){
				modifyAccount(customer.getCustom_no(), operater, channels);
			}
		}
	}
	
	/**
	 * 根据会员编号上送会员新的结算信息
	 * @param customerNo 会员编号
	 * @param operater 操作人
	 * @param channels 渠道，默认全部上送
	 */
	@Async
	public void modifyAccountByNo(String customerNo, String operater, String...channels){
		modifyAccount(customerNo, operater, channels);
	}
	
	/**
	 * 上送会员新的结算信息
	 * @param customerNo 会员编号
	 * @param operater 操作人
	 * @param channels 渠道，默认全部上送
	 */
	private void modifyAccount(String customerNo, String operater, String...channels){
		try{
			logger.info("{}，修改：{}结算信息，{}", operater, customerNo, channels);
			/*校验数据*/
			if(StringUtils.isBlank(customerNo) || StringUtils.isBlank(operater)){
				return;
			}
			/*未指定渠道，则获取全部进件的渠道*/
			if(channels == null || channels.length == 0){
				List<String> channelList = infChannelService.getChannelNoForIntoPieces();
				if(channelList != null && channelList.isEmpty()){
					return;
				}
				channels = channelList.toArray(new String[channelList.size()]);
			}
			/*遍历上送新的结算信息*/
			for(String channel : channels){
				NameValuePair[] data = {
						new NameValuePair(CUSTOMER_NO, customerNo),
						new NameValuePair(CHANNEL_NO, channel),
						new NameValuePair(OPERATER, operater),
						new NameValuePair(INTO_TYPE, MODIFY_ACCOUNT_TYPE)
				};
				intoPieces(data);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private String intoPieces(NameValuePair[] data){
		try{
			String result = HttpUtil.getHttpClientSslData(GlobalUtils.getConfProperty("mposinto.upload.url"), data);
			logger.info("进件参数：{}，返回信息：{}", data, result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(jsonObject == null){
				return SystemConstant.ERROR;
			}
			String respCode = jsonObject.getString("respCode");
			if(SystemConstant.RSPCODE_SUCCESS.equals(respCode)){
				return SystemConstant.SUCCESS;
			}
			return SystemConstant.ERROR;
		}catch(Exception e){
			e.printStackTrace();
			return SystemConstant.ERROR;
		}
	}
	
}